var express = require('express');
var router = express.Router();
var UserToken = require('../db/user_tokens.js');
var appkeyDao = require('../dao/appkey');
var appkeyRouter = module.exports;


/**
 * 生成
 * @param  token
 * @param  app_sid
 *         url//appkey/create
 */
appkeyRouter.createAppkey = function(req, res) {
    var par = req.body;
    if(!par.token) return res.json(new ERR('请先登录！', 403));
    if(!par.app_sid) return res.json(new ERR('app_sid不为空！', 400));
    UserToken.checkUserLogin(par.token, function(err, errCode, userInfo) {
        if(err) return res.json(new ERR(err, errCode));
        par.uid = userInfo.uid;
        par.account = userInfo.account;
        appkeyDao.createAppkey(par, function(e, data) {
            if(e) {
                res.json(new ERR(e, 400))
            } else {
                res.json(new PKG(data));
            }
        });
    });
}
router.post('/create', appkeyRouter.createAppkey);

module.exports = router;